<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="zh">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>住客入住</title>
    <link rel="stylesheet" href="/static/common/layui/css/layui.css">
    <link rel="stylesheet" href="/static/admin/css/style.css">
    <script src="/static/common/layui/layui.js"></script>
    <script src="/static/common/jquery-3.3.1.min.js"></script>
    <script src="/static/common/vue.min.js"></script>
</head>
<body onload="loadData();">
<div id="app">
    <!--顶栏-->
    <%@include file="/views/top.jsp" %>

    <div class="main">
        <!--左栏-->
        <div class="left">
            <ul class="cl">
                <!--顶级分类-->
                <li v-for="vo,index in menu" :class="{hidden:vo.hidden}">
                    <a href="javascript:;" :class="{active:vo.active}" @click="onActive(index)">
                        <i class="layui-icon" v-html="vo.icon"></i>
                        <span v-text="vo.name"></span>
                        <i class="layui-icon arrow" v-show="vo.url.length==0">&#xe61a;</i> <i v-show="vo.active"
                                                                                              class="layui-icon active">&#xe623;</i>
                    </a>
                    <!--子级分类-->
                    <div v-for="vo2,index2 in vo.list">
                        <a href="javascript:;" :class="{active:vo2.active}" @click="onActive(index,index2)"
                           v-text="vo2.name"></a>
                        <i v-show="vo2.active" class="layui-icon active">&#xe623;</i>
                    </div>
                </li>
            </ul>
        </div>
        <!--右侧-->
        <div class="right">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>添加住客</legend>
            </fieldset>
            <div class="layui-form-item">
                <label class="layui-form-label">住客姓名</label>
                <div class="layui-input-block">
                    <select name="name" class="layui-input" autocomplete="off" id="layui-form-selected" required>
                        <option value=""></option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">入住房间</label>
                <div class="layui-input-block">
                    <select name="h_type" class="layui-input" autocomplete="off" required>
                        <option value=""></option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">入住时间</label>
                <div class="layui-input-block">
                    <input type="datetime-local" name="enterTime" required autocomplete="off" class="layui-input" aria-required="true">
                    <span style="color: red" id="enterTimeSpan"></span>
                </div>
            </div>

<%--            <div class="layui-form-item">--%>
<%--                <label class="layui-form-label">退房时间</label>--%>
<%--                <div class="layui-input-block">--%>
<%--                    <input type="datetime-local" name="exitTime" required autocomplete="off" class="layui-input">--%>
<%--                </div>--%>
<%--            </div>--%>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" type="button" onclick="addGuests();">办理入住</button>
                    <button class="layui-btn layui-btn-danger" type="button" onclick="clearContent();">清空内容</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/static/admin/js/config.js"></script>
<script src="/static/admin/js/script.js"></script>
<script>

    function loadData() {
        // 用post请求获取会员和房间列表的信息
        $.post("/book", {"action": "loadData"}, function (data) {
            // 遍历数据并添加选项
            for (let i = 0; i < data.vips.length; i++) {
                // 添加会员信息
                let id = data.vips[i].id;
                let name = data.vips[i].name;
                $("select[name='name']").append("<option value='" + id + "'>" + name + "</option>");
            }
            for (let i = 0; i < data.homeList.length; i++) {
                // 添加房间信息
                let home_id = data.homeList[i].id;
                let home_num = data.homeList[i].num;
                let home_type = data.homeList[i].h_type;
                $("select[name='h_type']").append("<option value='" + home_id + "'>" + home_num + "--" + home_type + "</option>");
            }
        }, "json");
    }

    function addGuests() {
        // 获取表单数据的value值
        let vip_id = $("select[name='name']").val();
        let home_id = $("select[name='h_type']").val();
        let enterTime = $("input[name='enterTime']").val();
        //let exitTime = $("input[name='exitTime']").val();

        // 校验时间
        if (enterTime === "") {
            $("#enterTimeSpan").text("请选择入住时间");
            return;
        }else{
            $("#enterTimeSpan").text("");
            // 用异步的 post请求添加数据
            $.post("/book", {"action": "add", "vip_id": vip_id, "home_id": home_id, "enterTime": enterTime}, function (data) {
                console.log(data);
                if (data) {
                    alert("添加成功");
                } else {
                    alert("添加失败");
                }
            }, "json")
        }
    }

    function clearContent() {
        $("select[name='name']").val("");
        $("select[name='h_type']").val("");
        $("input[name='enterTime']").val("");
    }
</script>
</body>
</html>
